# python中的字典（类似于Java中的map集合）
# 字典可以准确的为各种真实的物体进行建模

# 字典用放在花括号 {} 中的一系列键 — 值对表示
alien_0 = {'color': 'green', 'points': '5'}
print(alien_0['color'] + " " + alien_0['points'])

# 为字典添加键值对
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)

# 创建字典
alien_1 = {}
alien_1['color'] = 'green'
alien_1['points'] = 5
print(alien_1)

# 修改字典中的值
alien_1['color'] = 'yellow'
print(alien_1)

# 删除键值对
del alien_1['color']
print(alien_1)

# 由类似对象组成的字典
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
print("我最喜欢的编程语言是：" + favorite_languages['sarah'].title())

# 遍历字典
for name, language in favorite_languages.items():
    print("\nName: " + name)
    print("Language: " + language)
print("\n")

# 遍历字典中的所有键
for key in favorite_languages.keys():
    print(key)
print("\n")
# 按顺序进行排序再遍历字典中的所有值
for key in sorted(favorite_languages.keys()):
    print(key)

# 遍历字典中的所有值
for language in favorite_languages.values():
    print(language)
print("\n")
# 遍历字典中的不同的值(重复值只输出一次)
for language in set(favorite_languages.values()):
    print(language)

# 需要将一系列字典存储在列表中，或将列表作为值存储在字典中，这称为 嵌套
alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'yellow', 'points': 10}
alien_2 = {'color': 'red', 'points': 15}
# 在列表中存储字典
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
    print(alien)
# 在字典中存储列表
pizza = {
    'crust': 'thick',
    'toppings': ['mushrooms', 'extra cheese'],
}
# 概述所点的比萨
print("You ordered a " + pizza['crust'] + "-crust pizza " + "with the following toppings:")
for topping in pizza['toppings']:
    print("\t" + topping)
print("\n")
# 在字典中存储字典
users = {
    'aeinstein': {
        'first': 'albert',
        'last': 'einstein',
        'location': 'princeton',
    },
    'mcurie': {
        'first': 'marie',
        'last': 'curie',
        'location': 'paris',
    },
}
for username, user_info in users.items():
    print("\nUsername: " + username)
    full_name = user_info['first'] + " " + user_info['last']
    location = user_info['location']
    print("\tFull name: " + full_name.title())
    print("\tLocation: " + location.title())
